package com.qqtech.qquan.share.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.ImgDomain;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseTypeDictEnum;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.ad.model.AdBaseVo;
import com.qqtech.qquan.ad.service.AdBaseService;
import com.qqtech.qquan.circle.enums.CircleTopicBizzTypeDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.mall.enums.MallConst;
import com.qqtech.qquan.mall.enums.MallProductStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallShopStyleDictEnum;
import com.qqtech.qquan.mall.model.MallProductQuery;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.mall.service.MallSupplierService;
import com.qqtech.qquan.order.model.OrderBaseVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.pub.model.ShareConfigBean;
import com.qqtech.qquan.share.model.ShareBase;
import com.qqtech.qquan.share.model.ShareBaseQuery;
import com.qqtech.qquan.share.service.ShareBaseService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.vote.model.VoteItemQuery;
import com.qqtech.qquan.vote.model.VoteItemVo;
import com.qqtech.qquan.vote.service.VoteBaseService;
import com.qqtech.qquan.vote.service.VoteItemService;
import com.qqtech.qquan.vote.service.VoteResultService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/share/base")
public class ShareBaseController extends BaseWebController<ShareBase, ShareBaseQuery> {
	@Resource
	private ShareBaseService shareBaseService;
    
	@Override
	protected BaseService<ShareBase> getBaseService() {
		return shareBaseService;
	}
	
	@Resource
	private OrderBaseService orderBaseService;
	
	@Resource
	private ShareConfigBean shareConfigBean;

	@Resource
	private CommunityMessageService communityMessageService;

	@Resource
	private CommunityBaseService communityBaseService;
	
	@Resource
	private CommunityJoinService communityJoinService;

	@Resource
	private ActivityBaseService activityBaseService;

	@Resource
	private MallProductService mallProductService;
	
	@Resource
	private MallSupplierService mallSupplierService;

	@Resource
	private MallShopService mallShopService;

	@Resource
	private CircleTopicService circleTopicService;

	@Resource
	private CircleBaseService circleBaseService;

	@Resource
	private DrawItemService drawItemService;

	@Resource
	private AdBaseService adBaseService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private VoteBaseService voteBaseService;

	@Resource
	private VoteResultService voteResultService;

	@Resource
	private VoteItemService voteItemService;
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/showShare")
	@ResponseBody
	public Result showShare(@RequestBody ShareBaseQuery shareBaseQuery) {
		Result result = new Result();
		Integer userId = super.getUserId();
		OrderBaseVo order=new OrderBaseVo();
		if(StringUtil.isNotBlank(shareBaseQuery.getOrderNo())){
			order=orderBaseService.getByOrderNo(shareBaseQuery.getOrderNo());
			shareBaseQuery.setBizzId(order.getId());
		}
		result=shareBaseService.showShare(shareBaseQuery.getBizzId(), shareBaseQuery.getBizzType(), userId);
		result.put("order", order);
		return result;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody ShareBase shareBase) {
		Result result = new Result();
		Integer userId = super.getUserId();
		shareBase.setUserId(userId);
		shareBaseService.addShare(shareBase);
		return result;
	}
	
	@RequestMapping(value = "/get")
	@ResponseBody
	public Result get() {
		Result result = new Result();
		result.put("shareConfigBean", shareConfigBean);
		return result;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer shareId, Integer shareType, Model model) {
		String title = "开启圈圈幸福生活";
		String content = "圈圈生活是一个基于兴趣爱好、社区邻里、姓氏家族形成集社交、媒体、电商以及技能服务为一体的生活综合服务平台。";
		List<ImgDomain> imgItem = new ArrayList<ImgDomain>();
		boolean hasImg = false;
		String showLogo="";
		String showName = "";
		UserBaseVo userBaseVo = null;
		if (shareType == 1) {// 社区大喇叭
			CommunityMessageVo detial = communityMessageService.getById(shareId);
			if (detial != null) {
				detial.setImgString(detial.getImg(), SysConst.IMG_POSTFIX);
				title = "社区大喇叭";
				content = detial.getContent();
				imgItem = detial.getBigImgItems();
				hasImg = detial.isHasImg();
			}
			CommunityBaseVo communityBaseVo = communityBaseService.getById(detial.getCommunityId());
			showName = communityBaseVo.getName();
			userBaseVo = userBaseService.getById(detial.getCreatorId());
		} else if (shareType == 2) {// 活动
			ActivityBaseVo detial = activityBaseService.getById(shareId);
			if (detial != null) {
				detial.setImgString(detial.getResumeImg(), SysConst.IMG_POSTFIX);
				title = "活动-" + detial.getName();
				content = detial.getResume();
				imgItem = detial.getBigImgItems();
				hasImg = detial.isHasImg();
				if (detial.getType() == ActivityBaseTypeDictEnum.CIRCLE.getCode()) {
					CircleBaseVo circleBaseVo = circleBaseService.getById(detial.getBizzId());
					showLogo=circleBaseVo.getBigFullLogo();
					showName = circleBaseVo.getName();
				} else if (detial.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {
					CommunityBaseVo communityBaseVo = communityBaseService.getById(detial.getBizzId());
					showName = communityBaseVo.getName();
				}
				userBaseVo= userBaseService.getById(detial.getCreatorId());
			}
		} else if (shareType == 3) {// 商城产品
			MallProductVo detial = mallProductService.getById(shareId);
			if (detial != null) {
				detial.setImgString(detial.getResumeImg(), SysConst.IMG_POSTFIX);
				title = "商城-" + detial.getName();
				content = detial.getSubtitle();
				imgItem = detial.getBigImgItems();
				hasImg = detial.isHasImg();
				MallShopVo mallShopVo = mallShopService.getById(detial.getShopId());
				showName = mallShopVo.getName();
			}
		} else if (shareType == 4) {// 圈子话题
			CircleTopicVo detial = circleTopicService.getById(shareId);
			if (detial != null) {
				detial.setImgString(detial.getImg(), SysConst.IMG_POSTFIX);
				title = "圈子话题";
				content = detial.getContent();
				imgItem = detial.getBigImgItems();
				hasImg = detial.isHasImg();
				CircleBaseVo circleBaseVo = circleBaseService.getById(detial.getCircleId());
				showName = circleBaseVo.getName();
				showLogo=circleBaseVo.getBigFullLogo();
				userBaseVo= userBaseService.getById(detial.getCreatorId());
				if (detial.getBizzType() == CircleTopicBizzTypeDictEnum.VOTE.getCode()) {
					VoteItemQuery voteItemQuery = new VoteItemQuery();
					voteItemQuery.setVoteId(detial.getBizzId());
					List<VoteItemVo> voteItemList = voteItemService.queryList(voteItemQuery);
					Integer voteItemResult;
					for (VoteItemVo voteItemVo : voteItemList) {
						voteItemResult = voteResultService.getVoteItemResult(voteItemQuery.getVoteId(),
								voteItemVo.getId());
						voteItemVo.setPercent(voteItemResult);
					}
					model.addAttribute("voteItemList", voteItemList);
				}
			}
		} else if (shareType == 5) {// 抽奖产品
			return "/lottery/lottery";
		} else if (shareType == 6) {// 广告
			AdBaseVo detial = adBaseService.getById(shareId);
			if (detial != null) {
				detial.setImgString(detial.getImg(), SysConst.IMG_POSTFIX);
				title = detial.getTitle();
				content = detial.getContent();
				imgItem = detial.getBigImgItems();
				hasImg = detial.isHasImg();
			}
		} else if (shareType == 7) {// 社区抽奖广告
			return "/lottery/lottery";
		} else if (shareType == 8) {// 圈商
			MallShopVo detial = mallShopService.getById(shareId);
			if(MallShopStyleDictEnum.LOCAL.getCode()==detial.getStyle()){
				title = "周边商家";
			}else if(MallShopStyleDictEnum.VIRTUAL.getCode()==detial.getStyle()){
				title = "邻里店";
			}else{
				title = "圈商";
			}
			content = detial.getResume();
			showName = detial.getName();
			showLogo=detial.getFullLogo();
			MallProductQuery query=new MallProductQuery();
			query.setShopId(shareId);
			query.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
			Pageable pageKit = new PageKit(1, 10,new PageOrder(Direction.DESC, "id"));
			List<MallProductVo> productlist=mallProductService.queryList(query, pageKit);
			for (MallProductVo mallProductVo : productlist) {
				mallProductVo.setImgString(mallProductVo.getImg(),MallConst.IMG_SUPPLIER_FIRST);
			}
			model.addAttribute("productlist", productlist);
			
		} else if (shareType == 9) {// 订单分享
			return "/order/order";
		}
		CommunityBaseVo communityBaseVo=new CommunityBaseVo();
		if(null!=userBaseVo){
			if(StringUtil.isNotBlank(userBaseVo.getInterests())){
				String[] interests=userBaseVo.getInterests().split(",");
				model.addAttribute("interests", interests);
			}
			if(null!=userBaseVo.getCommunityId()){
				communityBaseVo=communityBaseService.getById(userBaseVo.getCommunityId());
			}
		}
		model.addAttribute("communityBaseVo", communityBaseVo);
		model.addAttribute("userBaseVo", userBaseVo);
		model.addAttribute("showLogo", showLogo);
		model.addAttribute("showName", showName);
		model.addAttribute("shareType", shareType);
		model.addAttribute("title", title);
		model.addAttribute("content", content);
		model.addAttribute("imgItem", imgItem);
		model.addAttribute("hasImg", hasImg);
		return "/share/detail";
	}
}
